﻿#include "TlvCmdPanel.h"
#include "ui_TlvCmdPanel.h"
#include "services/Task/TlvCmdTask.h"
#include "services/Task/TlvCodeTask.h"
#include "services/tlv/Com/TlvThreadCom.h"
#include "components/Toast/Toast.h"

#include <QList>
#include "components/FWidgets/FMessageBox/FMessageBox.h"

TlvCmdPanel::TlvCmdPanel(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TlvCmdPanel)
{
    ui->setupUi(this);
    QList<int> sizes;
    sizes << width() * 0.6 << width() * 0.4;
    ui->splitter->setSizes(sizes);

    ui->btnIICSendSelected->setOnIcon(QIcon(":/imgs/stop.png"));
    ui->btnTlvSendSelected->setOnIcon(QIcon(":/imgs/stop.png"));

    TlvCmdTask* task = TlvCmdTask::instance();
    connect(task, &TlvCmdTask::taskFinished, this, &TlvCmdPanel::onTaskDone);
    connect(task, &TlvCmdTask::taskStopped, this, &TlvCmdPanel::onTaskDone);
}

TlvCmdPanel::~TlvCmdPanel()
{
    delete ui;
}

void TlvCmdPanel::setTitle(const QString &title)
{
    m_title = title;
}

IICTableWidget *TlvCmdPanel::iicTable()
{
    return ui->twIIC;
}

TlvTableWidget *TlvCmdPanel::tlvTable()
{
    return ui->twTlv;
}

CmdPageModel TlvCmdPanel::toCmdPageModel()
{
    CmdPageModel res;
    res.name = m_title;
    res.iic = ui->twIIC->toIICCmdModel();
    res.tlv = ui->twTlv->toTlvCmdModel();
    return res;
}

void TlvCmdPanel::handleSendSelected(bool isIIC)
{
    IconButton* btn = ui->btnTlvSendSelected;
    if (isIIC) btn = ui->btnIICSendSelected;

    TlvCmdTask* task = TlvCmdTask::instance();
    if (btn->isOn() && task->isRunning()) {  // 这是判断运行的任务是否是当前选择的任务
        task->stop();
        btn->setIconStatus(false);
        Toast::info( ITask::INFO_TASK_STOPPED(task->taskName()) );
        return;
    }
    if (task->isRunning()) {  // 其它Cmd任务正在运行
        Toast::warning( ITask::WARN_TASK_IS_RUNNING(task->taskName()) );
        return;
    }
    if (TlvCodeTask::instance()->isRunning()) {  // Code任务正在运行
        Toast::warning( ITask::WARN_TASK_IS_RUNNING(TlvCodeTask::instance()->taskName()) );
        return;
    }
    if (!TlvThreadCom::instance()->isOpen()) {  // 通讯设备未连接
        Toast::error(ICom::ERR_NOT_OPEN());
        return;
    }

    QSpinBox* box = isIIC ? ui->sbIICLoopNum : ui->sbTLVLoopNum;
    int cnt = box->value();
    box->setEnabled(false);
    task->setRunCnt(cnt);

    if (isIIC) {
        task->setTaskName(QString("Command.%1.iic").arg(m_title));
        if (ui->twIIC->sendSelected())  btn->setIconStatus(true);
        else box->setEnabled(true);
    } else {
        task->setTaskName(QString("Command.%1.tlv").arg(m_title));
        if (ui->twTlv->sendSelected()) btn->setIconStatus(true);
        else box->setEnabled(true);
    }

}

void TlvCmdPanel::onTaskDone()
{
    ui->btnIICSendSelected->setIconStatus(false);
    ui->btnTlvSendSelected->setIconStatus(false);
    ui->sbIICLoopNum->setEnabled(true);
    ui->sbTLVLoopNum->setEnabled(true);
}

void TlvCmdPanel::on_btnIICSendSelected_clicked()
{
    if (ui->twIIC->checkedRowsCount() == 0) {
        Toast::warning(tr("No IIC cmd selected!"));
        return;
    }
    handleSendSelected(true);
}

void TlvCmdPanel::on_btnIICAddRow_clicked()
{
    for (int i = 0; i < ui->twIIC->rowCount(); i++) {
        if (ui->twIIC->isRowChecked(i)) {
            ui->twIIC->insertNewLine(i);
            return;
        }
    }
    ui->twIIC->addNewLine();
}

void TlvCmdPanel::on_btnIICDeleteRows_clicked()
{
    if (FMessageBox::Yes != FMessageBox::question(this, tr("Question"), tr("Sure to remove selected row(s)?")))
        return;
    int num = 0;
    // 要从后往前删除，否则会索引变化
    for (int i = ui->twIIC->rowCount() - 1; i >= 0; i--) {
        if (ui->twIIC->isRowChecked(i)) {
            ui->twIIC->removeRow(i);
            num++;
        }
    }
    if (num == 0) {
        Toast::warning(tr("No IIC cmd selected!"));
    } else {
        Toast::success(tr("%1 IIC cmd has been removed.").arg(num));
    }
}


void TlvCmdPanel::on_btnTlvSendSelected_clicked()
{
    if (ui->twTlv->checkedRowsCount() == 0) {
        Toast::warning(tr("No Tlv cmd selected!"));
        return;
    }
    handleSendSelected(false);
}


void TlvCmdPanel::on_btnTLVAddRow_clicked()
{
    for (int i = 0; i < ui->twTlv->rowCount(); i++) {
        if (ui->twTlv->isRowChecked(i)) {
            ui->twTlv->insertNewLine(i);
            return;
        }
    }
    ui->twTlv->addNewLine();
}


void TlvCmdPanel::on_btnTLVDeleteRows_clicked()
{
    if (FMessageBox::Yes != FMessageBox::question(this, tr("Question"), tr("Sure to remove selected row(s)?")))
        return;
    int num = 0;
    // 要从后往前删除，否则会索引变化
    for (int i = ui->twTlv->rowCount() - 1; i >= 0; i--) {
        if (ui->twTlv->isRowChecked(i)) {
            ui->twTlv->removeRow(i);
            num++;
        }
    }
    if (num == 0) {
        Toast::warning(tr("No Tlv cmd selected!"));
    } else {
        Toast::success(tr("%1 Tlv cmd has been removed.").arg(num));
    }
}

